const fs = require('fs')
const path = require('path')
const DictHeader = require('./dict_header')
const TextDictMaker = require('./text_dict_maker')
const ImageDictMaker = require('./image_dict_maker')
const dict_config_keys = require('../dict_config_keys')
const Dict = require('../dict')

async function generate_maker(dict_folder, dst_folder, options) {
    // dict.txt
    const header = new DictHeader(path.join(dict_folder, 'dict.txt'), dict_config_keys.description, true);
    if (!header.isValid()) {
        return null
    }

    let maker = null;
    if (header.isTextType()) {
        maker = new TextDictMaker(header, dict_folder, dst_folder, options);
    }
    else if (header.isImageType()) {
        maker = new ImageDictMaker(header, dict_folder, dst_folder, options);
    }

    if (maker == null) return null;
    maker.beforeMake();
    return maker;
}

async function proxy_make(dict_folder, dst_folder, dst_name) {
    const maker = await generate_maker(dict_folder, dst_folder, { dst_name });
    if (!maker) return null;


    // assets
    // replace.txt
    // parts
    // plugin - plugin.js

    // return dst_path
    return await maker.make();
}


async function proxy_make_debug_dict(src_folder) {
    const maker = await generate_maker(src_folder, null, { debug: true });
    if (!maker) return null;
    const dictPath = await maker.make();
    return {
        maker,
        dict: new Dict(dictPath, { debug: true, debugDir: src_folder })
    }
}

module.exports = {
    proxy_make,
    proxy_make_debug_dict
};